<?php

class ClaimteController extends BaseController {

	public function formClaimte()
	{
		// show the form
		return View::make('formclaimte');
	}

	public function editClaimte()
	{

		// show the form
		$data = DB::table('tbl_clm_te')->where('id',$id)->get();
		return View::make('edit_claimte')->with('data', $data);

	}

	/*
	public function save1Claimte()
	{
		$model = new Claimte;
		$model->date_claim = Input::get('date');
		$model->employeename = Input::get('employeename');
		
		
		if(isset($model->employeename)) {

			try {

				$transaction = DB::transaction(function()
				{
					$data = Input::get('employeename');

					if ($model->save())
					{
						print_r($data);
						$post = Input::all();
						if (isset($post))
						{
							for ($i=0; $i < count($model->date_claim); $i++) 
							{
								$model= new Claimte;
								$data=array();
								$data['employeename']=$model->employee_name['employeeid'][0];
								$data['date']=$model->date_claim['date'][$i];
								$data['date']=$model->destination['destination'][$i];
								$data['km']=$model->km['km'][$i];
								$data['toll_km']=$model->toll_km['date'][$i];
								$data['amount_idr']=$model->amount_idr['amount_idr'][$i];
								if(!$model->save()){
									$transaction->rollBack();
								}

							}//endfor
							$transaction->commit();
							return Redirect::to('formclaimte');

						}
					}//endifmodel 
				});//end function;
			}//endtry 
			catch(\Exception $e){
				$transaction->rollBack();
				echo $e = 'Error Coy';
			}//endcatch
		}//endif
	}

	*/

	public function saveClaimte()
	{
		$input = Input::all();
		$master = new Claimmstr;		
		$master->employee_name = $input['employeeid'];
		$master->save();


		for ($i=0; $i < count(Input::get('date')); $i++)
		{
			$claimdetail = new Claimte;
			$claimdetail->id_mstr_clm = $master->id;
			$dc = $input['date'][$i];
			$dt = str_replace('/', '-', $dc);
			$claimdetail->date_claim = date('Y-m-d',strtotime($dt));
			$claimdetail->destination = $input['destination'][$i];
			$claimdetail->amount_idr = $input['amount_idr'][$i];
			// echo "<pre>";

			// print_r($model);
			// echo "</ pre>";

			$claimdetail->save();
		}
		// /exit();
        return Redirect::to('/formclaimte')->with('message', 'Save Success'); 
	}

	public function viewClaimte()
	{
		$search = Input::get('search');
		//$stat = Input::get('stat');
		if (!empty($search)) {

			$data = DB::table('tbl_clm_mstr')
				->select(DB::raw('count(tbl_clm_te.date_claim) as jumlah_claim, tbl_clm_mstr.employee_name,tbl_clm_mstr.created_at, tbl_clm_mstr.id'))
				->leftJoin('tbl_clm_te', 'tbl_clm_mstr.id', '=', 'tbl_clm_te.id_mstr_clm','and','tbl_clm_mstr.created_at', '=', 'tbl_clm_te.created_at')
				->orwhere('tbl_clm_mstr.employee_name','LIKE','%'.$search.'%')
				->groupBy('tbl_clm_mstr.created_at','tbl_clm_te.created_at')
				->orderBy('tbl_clm_mstr.created_at', 'asc')
				->paginate(50);			
		} else {
			$data = DB::table('tbl_clm_mstr')
				->select(DB::raw('count(tbl_clm_te.date_claim) as jumlah_claim, tbl_clm_mstr.employee_name,tbl_clm_mstr.created_at, tbl_clm_mstr.id'))
				->leftJoin('tbl_clm_te', 'tbl_clm_mstr.id', '=', 'tbl_clm_te.id_mstr_clm','and','tbl_clm_mstr.created_at', '=', 'tbl_clm_te.created_at')
				->groupBy('tbl_clm_mstr.created_at','tbl_clm_te.created_at')
				->orderBy('tbl_clm_mstr.created_at', 'asc')
				->paginate(50);
		}
		return View::make('viewclaimte')->with('data',$data);


		///
		$q = Input::get('q');
		$stat = Input::get('stat');
		if (!empty($q)) {
			$data = DB::table('tbl_dds')->orderBy('i_dds_id', 'desc')
			->leftJoin('users', 'tbl_dds.i_user_create', '=', 'users.i_user_id')
			->orwhere('v_dds_sn','LIKE','%'.$q.'%')
			->orwhere('v_dds_name','LIKE','%'.$q.'%')
			->paginate(50);
		}elseif (!empty($stat)) {
			$data = DB::table('tbl_dds')->orderBy('i_dds_id', 'desc')
			->where('v_status',$stat)
			->paginate(50);
		}else{
			$data = DB::table('tbl_dds')->orderBy('i_dds_id', 'desc')
			->paginate(50);
		}
				return View::make('view_dds')->with('data', $data);

	}

	public function viewClaimte1()
	{

		PDF::SetTitle('Hello World');

		PDF::AddPage();

		PDF::Write(0, 'Hello World');

		PDF::Output('hello_world.pdf');

	}

}

	
